import 'package:flutter/material.dart';
import 'package:flutter_demo/apply.dart';
import 'package:flutter_demo/dialog/change_pwd_dialog.dart';
import 'package:flutter_demo/other/application.dart';
import 'package:flutter_demo/personal_info.dart';
import 'package:flutter_demo/search_apply.dart';
import 'package:flutter_demo/see_apply.dart';
import 'package:flutter_demo/widget/buttons.dart';
import 'package:flutter_demo/widget/tiny_widgets.dart';

class HomeScreen extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            width: double.infinity,
            height: 60,
            padding: EdgeInsets.symmetric(
              horizontal: 195,
            ),
            decoration: BoxDecoration(
              color: Color(0xff1a56a8),
            ),
            child: Container(
              width: 580,
              height: 59,
              alignment: Alignment.centerLeft,
              child: Image(
                image: NetworkImage('http://my.gxrczc.com/assets/img/logo_zc.png'),
              ),
            ),
          ),
          Expanded(
            child: Container(
              width: double.infinity,
              height: double.infinity,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: NetworkImage(
                    'http://my.gxrczc.com/assets/img/bg.png',
                  ),
                  fit: BoxFit.cover,
                ),
              ),
              child: content(context),
            ),
          ),
        ],
      ),
    );
  }

  Widget content(BuildContext context) {
    return Container(
      width: 1138,
      height: 335.28,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 853.5,
            height: 335.28,
            padding: EdgeInsets.only(
              left: 20,
              right: 10,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                section(),
                Container(
                  width: double.infinity,
                  height: 0.5,
                  decoration: BoxDecoration(
                    color: Color(0xffdedede),
                  ),
                ),
                C(19.5),
                Expanded(
                  child: Wrap(
                    spacing: 10,
                    runSpacing: 20,
                    children: showItems(context),
                  ),
                ),
                C(20),
              ],
            ),
          ),
          userSection(context),
        ],
      ),
    );
  }

  List<Widget> showItems(BuildContext context) {
    if (Application.currentType == AccountType.person) {
      return [
        item('http://my.gxrczc.com/assets/img/ico/b_rending.jpg',
            '职称申报', onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => ApplyScreen()));
            }),
        item('http://my.gxrczc.com/assets/img/ico/b_shenyan.jpg',
            '查看申报状态', onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => SeeApplyScreen()));
            }),
      ];
    } else {
      return [
        item('http://my.gxrczc.com/assets/img/ico/b_shenyan.jpg',
            '审核申报', onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => SearchApplyScreen()));
            }),
      ];
    }
  }

  Widget section() {
    return Container(
      width: double.infinity,
      height: 75.28,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            child: Align(
              alignment: Alignment.centerLeft,
              child: Text(
                '职称业务办理',
                style: TextStyle(
                  color: Color(0xff1956a9),
                  fontSize: 24,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget item(String iconUrl, String title, {void Function()? onTap}) {
    return InkWell(
      onTap: onTap,
      child: Container(
        width: 267.83,
        height: 100,
        padding: EdgeInsets.all(20),
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5),
          border: Border.all(color: Color(0xffdedede), width: 1),
        ),
        child: Row(
          children: [
            Image(
              image: NetworkImage(iconUrl),
              width: 58,
              height: 58,
              fit: BoxFit.cover,
            ),
            C(20),
            Text(
              title,
              style: TextStyle(
                color: Color(0xff333333),
                fontWeight: FontWeight.bold,
                fontSize: 18,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget userSection(BuildContext context) {
    return Container(
      width: 283.5,
      height: 97.85,
      padding: EdgeInsets.all(20),
      decoration: BoxDecoration(
        border: Border(
          left: BorderSide(color: Color(0xff2399f1), width: 2),
          bottom: BorderSide(color: Color(0xffdedede), width: 1),
        ),
      ),
      child: Stack(
        children: [
          Positioned(
            left: 0,
            top: 0,
            width: 45,
            height: 45,
            child: Image(
              image: NetworkImage('http://my.gxrczc.com/assets/img/head.png'),
              width: 45,
              height: 45,
            ),
          ),
          Positioned(
            left: 60.38,
            top: 0,
            width: 62.11,
            height: 17.6,
            child: Text(
              Application.currentAccount,
              style: TextStyle(
                color: Color(0xff1956a9),
                fontSize: 16,
              ),
            ),
          ),
          Positioned(
            left: 60.38,
            top: 27.6,
            width: 68,
            height: 24,
            child: BaseButton(
              radius: 5,
              width: 68,
              height: 24,
              withShadow: false,
              buttonColor: Color(0xff1956a9),
              onTap: () => Navigator.push(context,
                  MaterialPageRoute(builder: (context) => PersonalInfoScreen())),
              child: Text(
                '我的信息',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                ),
              ),
            ),
          ),
          Positioned(
            left: 136.38,
            top: 27.6,
            width: 68,
            height: 24,
            child: BaseButton(
              radius: 5,
              width: 68,
              height: 24,
              withShadow: false,
              buttonColor: Color(0xff353734),
              onTap: () => showChangePwdDialog(context),
              child: Text(
                '修改密码',
                style: TextStyle(
                  color: Color(0xffe4d68f),
                  fontSize: 12,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}